﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace COC.GameLogic
{
	public class ShootDetecter
	{
		List<PlayerActor> m_Actors = new List<PlayerActor>();

		public ShootDetecter()
		{
		}

		public void AddActor(PlayerActor item)
		{
			m_Actors.Add(item);
		}

		public void RemoveActor(string actorname)
		{
			PlayerActor toremove = null;
			foreach (PlayerActor actor in m_Actors)
			{
				if(actor.Name==actorname)
				{
					toremove = actor;
					break;
				}
			}
			if (toremove == null)
				return;
			m_Actors.Remove(toremove);
		}

		public string TestShootImmediate(Ray ray, int damage)
		{
			foreach (PlayerActor actor in m_Actors)
			{
				foreach (ModelMesh mesh in actor.Model.Model.Meshes)
				{
					Vector3 nowPos = Vector3.Transform(
						mesh.BoundingSphere.Center, actor.LocalToWorldMat);
					if (new BoundingSphere(nowPos, mesh.BoundingSphere.Radius).Intersects(ray) != null)
					{
						actor.OnShoot(damage);
						return mesh.Name;
					}
				}
			}
			return null;
		}

		public void Clear()
		{
			m_Actors.Clear();
		}
	}
}